package be.bonamis.timesheet.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * An entity class which contains the information of a single person.
 * 
 * @author Petri Kainulainen
 */
@Entity
@Table(name = "persons")
public class Person extends PersistentObject {

	private static final long serialVersionUID = 1L;

	@Column(name = "first_name", nullable = false)
	private String firstName;

	@Column(name = "last_name", nullable = false)
	private String lastName;

	@Column(name = "initials", nullable = false)
	private String initials;

	/**
	 * Gets a builder which is used to create Person objects.
	 * 
	 * @param firstName
	 *            The first name of the created user.
	 * @param lastName
	 *            The last name of the created user.
	 * @return A new Builder instance.
	 */
	public static Builder getBuilder(String firstName, String lastName,
			String initials) {
		return new Builder(firstName, lastName, initials);
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getInitials() {
		return initials;
	}

	/**
	 * Gets the full name of the person.
	 * 
	 * @return The full name of the person.
	 */
	@Transient
	public String getName() {
		StringBuilder name = new StringBuilder();

		name.append(firstName);
		name.append(" ");
		name.append(lastName);

		return name.toString();
	}

	public void update(String firstName, String lastName, String initials) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.initials = initials;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	/**
	 * A Builder class used to create new Person objects.
	 */
	public static class Builder {
		Person built;

		/**
		 * Creates a new Builder instance.
		 * 
		 * @param firstName
		 *            The first name of the created Person object.
		 * @param lastName
		 *            The last name of the created Person object.
		 */
		Builder(String firstName, String lastName, String initials) {
			built = new Person();
			built.firstName = firstName;
			built.lastName = lastName;
			built.initials = initials;
		}

		/**
		 * Builds the new Person object.
		 * 
		 * @return The created Person object.
		 */
		public Person build() {
			return built;
		}
	}

}
